home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libcan / canfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  1.8 KB  |  74 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    canfile -
  19.  *        Read and write canvases on stdin and stdout
  20.  *
  21.  *                Paul Haeberli - 1992
  22.  *
  23.  *    exports
  24.  *
  25.         void canvastofile(c,f);
  26.         canvas *filetocanvas(f);
  27.  *
  28.  */
  29. #include "stdio.h"
  30. #include "canvas.h"
  31.  
  32. #define CANVASMAGIC    (0x69313232)
  33. static canvas *rc;
  34.  
  35. canvastofile(c,f)
  36. canvas *c;
  37. FILE *f;
  38. {
  39.     long magic;
  40.  
  41.     magic = CANVASMAGIC;
  42.     fwrite(&magic,sizeof(long),1,f);
  43.     fwrite(c,sizeof(canvas),1,f);
  44.     fwrite(c->data,c->xsize*c->ysize*sizeof(long),1,f);
  45.     fflush(f);
  46. }
  47.  
  48. canvas *filetocanvas(f)
  49. FILE *f;
  50. {
  51.     long magic;
  52.     unsigned long *data;
  53.     canvas tc;
  54.  
  55.     magic = CANVASMAGIC;
  56.     if(!fread(&magic,sizeof(long),1,f))
  57.     return 0;
  58.     if(magic != CANVASMAGIC) 
  59.     return 0;
  60.     if(!fread(&tc,sizeof(canvas),1,f))
  61.         return 0;
  62.     if(!rc || rc->xsize != tc.xsize ||  rc->ysize != tc.ysize) {
  63.     if(rc)
  64.         freecanvas(rc);
  65.     rc = newcanvas(tc.xsize,tc.ysize);
  66.     data = rc->data;
  67.     *rc = tc;
  68.     rc->data = data;
  69.     }
  70.     if(!fread(rc->data,rc->xsize*rc->ysize*sizeof(long),1,f))
  71.         return 0;
  72.     return rc;
  73. }
  74.